<template>
<div class="goods-box">
  <!--面包屑-->
  <el-breadcrumb separator="/">
    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item><a href="/">商品管理</a></el-breadcrumb-item>
    <el-breadcrumb-item>商品列表</el-breadcrumb-item>
  </el-breadcrumb>
  <!--商品列表部分-->
  <el-card>
    <!--商品操作部分-->
    <el-row :gutter="20">
      <el-col :span="6">
        <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable class="input-with-select" @clear="getGoodsList">
          <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
        </el-input>
      </el-col>
      <el-col :span="4">
        <el-button  type="primary" @click="addProduct">添加商品</el-button>
      </el-col>
    </el-row>
    <!--商品列表表格-->
    <el-row>
      <el-table
        :data="goodsList"
        border
        style="width: 100%">
        <el-table-column
          label="#"
          type="index"
          width="50"
        >
        </el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称"
          >
        </el-table-column>
        <el-table-column
          prop="goods_price"
          label="商品价格"
          width="180">
        </el-table-column>
        <el-table-column
          prop="goods_weight"
          label="商品重量"
          width="180"
        >
        </el-table-column>
        <el-table-column
          label="创建时间"
          width="180"
        >
          <template v-slot="scope">
            {{scope.row.add_time | dateFilter}}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="200"
        >
          <template v-slot="scope">
            <!--编辑商品信息-->
            <el-button type="primary" icon="el-icon-edit" size="medium"
                       @click="editProduct(scope.row.goods_id)"></el-button>
            <!--删除商品信息-->
            <el-button type="danger" icon="el-icon-delete" size="medium"
                       @click="deleteProduct(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-row>
  </el-card>
</div>
</template>

<script>
import params from "@/components/goods/params";

export default {
  name: 'goods',
  data(){
    return{
      // 商品列表数据
      goodsList: [],
      // 获取商品列表数据总条数
      total: 0,
      // 查询参数
      queryInfo:{
        // 查询参数
        query:'',
        // 当前页码
        pagenum: 1,
        // 分页条数
        pagesize: 10,
      }
    }
  },
  created() {
    // 请求商品列表数据
    this.getGoodsList()
  },
  methods:{
    // 获取商品列表
    async getGoodsList(){
      const {data: res} = await this.$http.get(`goods`, {params: this.queryInfo})
      if(res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 绑定表格数据
      this.goodsList = res.data.goods
      // 绑定分页数据
      this.total = res.data.total
    },
    // 添加商品
    addProduct(){
      this.$router.push('/goods/addProduct')
    },
    // 分页条数
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      // 修改每页显示多少条
      this.queryInfo.pagesize = val
      // 修改每页显示多少条后重新请求数据
      this.getGoodsList()
    },
    // 分页页码
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      // 修改当前页码
      this.queryInfo.pagenum = val
      // 修改当前页码后重新请求数据
      this.getGoodsList()
    },
    // 编辑商品信息
    editProduct(id){
      console.log(`操作商品的id是${id}`)
    },
    // 删除商品信息
    async deleteProduct(id){
      console.log(`操作商品的id是${id}`)
      const message = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch((error) => {
        return error
      })
      // 如果用户点击取消会关闭
      if(message !== "confirm"){
        return this.$message.info('已经取消删除操作')
      }
      // 否则执行删除操作
      const {data: res} = await this.$http.delete(`goods/${id}`)
      // 删除失败提示
      if(res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 删除成功提示
      this.$message.success(res.meta.msg)
      // 重新请求数据
      this.getGoodsList()
    },
  }
}
</script>

<style lang="scss" scoped>

</style>
